<?php
/*
	首页
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($return['pluginset']['is_guoshen'] && $return['pluginset']['guoshen_url'] && in_array($_SESSION['container'],array('baiduapp','aliapp','ttapp','wxapp'))){
	exi('','',$return['pluginset']['guoshen_url']);	
}
if($_RQ['op']=='main'){
	$return['welcomehdps']=$return['pluginset']['welcomehdps'];
	if(count($return['welcomehdps'])>0 && $return['pluginset']['iswelcome']==1){
		$return['iswelcome']=$this->session('iswelcome')?0:1;
		if($return['iswelcome']){
			$this->session('iswelcome',1);
		}
	}
	$return['pluginset']['searchkeyword']=$return['pluginset']['searchkeyword']?explode('|',$return['pluginset']['searchkeyword']):array('旅游','上门服务','家政服务','拼团');
	$return['member']=cfc('core')->get_member($_SESSION['uid']);
	$pindex = max(1, intval($post['page']));
	$psize = 10;
	$total='';
	$shopset=plugin('jy_weishop')->plugin_setting('setting');
	$sale_luck=plugin('jy_weishop')->plugin_setting('sale_luck');
	if($sale_luck['isopen']==1){
			if(isset($_RQ['luckkey'])){
				$return['sale_luck']=model('jy_weishop/activity/sales')->jieluck($_RQ['luckkey']);
			}else{
				$return['sale_luck']=model('jy_weishop/activity/sales')->getluck();
			}
	}
	if(!$return['sale_luck']){
			$isshow_sale_offer=$this->session('isshow_sale_offer');
			if(!$isshow_sale_offer){
				$return['sale_offer']=model('jy_weishop/activity/sales')->getoffer();
			}
			if(!$return['sale_offer']){
				if($shopset['no_ordermodal']!=1){
					$return['orderlist']=pdo_getslice('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'deleted'=>0,'status'=>0),array($pindex,$psize),$total,'','','createtime desc');
					foreach($return['orderlist'] as &$ov){
						$ordergoods=pdo_get('jy_weishop_order_goods',array('orderid'=>$ov['id'],'uniacid'=>$_SESSION['uniacid']),array('goodsid','optionid','price','total','goodssn','productsn'));
						$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$ordergoods['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','image','title','merchid'));
						$ov['goods']=array_merge($ordergoods,$goodsinfo);
						$ov['createtime']=date('Y/m/d H:i:s',$ov['createtime']);
						$ov['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$ov['id'],'uniacid'=>$_SESSION['uniacid']),'total');
					}
					$return['allprice']=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'deleted'=>0,'status'=>0),'price');
				}
				if(empty($return['orderlist'])){
					$return['coupon_wait']=model('jy_weishop/task')->pop_coupon_wait();
					$return['coupon_wait']=$return['coupon_wait']?$return['coupon_wait'][0]:false;
					if(!$return['coupon_wait']){
						$return['coupon_hd']=model('jy_weishop/task')->pop_coupon_hd();
						if(!$return['coupon_hd']){
							$return['game_wait']=model('jy_weishop/task')->pop_game();
						}
					}
				}
			}else{
					$this->session('isshow_sale_offer',1);
			}
	}
	$sharelink='jy_city/index//fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>$return['pluginset']['share_title']?$return['pluginset']['share_title']:'首页-在线'.$return['pluginset']['buytitle'],
			'desc'=>$return['pluginset']['share_desc']?$return['pluginset']['share_desc']:'智慧同城，一个新概念的同城系统！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>ATTACHMENT_URL.$return['pluginset']['share_image']
	);
	$return['list']=array();
	$return['pcd']=$this->session('pcd')?:array('city'=>'');
	$bankuai1=array();
	$bankuai3=array();
	/*foreach($return['pluginset']['bankuai'] as $k=>$b){
		$b['action'] .='&bankuaiid='.$k;
		if($b['showtype']==$return['showtype']){
			$bankuai3[]=$b;
			break;
		}
	}*/
	foreach($return['pluginset']['bankuai'] as $kv=>$bv){
		$bv['action'] .='&bankuaiid='.$kv;
		if($bv['hot']==1 && count($bankuai1)<=4){
			$bankuai1[]=$bv;
		}else{
			$bankuai3[]=$bv;
		}
	}
	foreach($return['pluginset']['bankuai'] as $kv=>&$bv){
		$groupids=str_array($bv['merchgroups']);
		if(count($groupids)>0){
			$bv['childs']=pdo_getall('jy_merch_category',array('id'=>$groupids,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	$return['childs']=$return['allcate']=pdo_getall('jy_merch_category',array('uniacid'=>$_SESSION['uniacid']));
	//$bankuai3[]=array('title'=>'全部分类','icon'=>'icon-classification2','color'=>'#ffc350','showtype'=>'0','action'=>'jy_city/merchlist//isbankai=1&all=1');
	$return['setting']=array('hdps'=>$return['pluginset']['hdps'],'issearch'=>1,'bankuai1'=>$bankuai1,'bankuai3'=>$bankuai3);
	$return['feemoney']=plugin('jy_weishop')->plugin_setting('feemoney');
	$return['params']=array('order'=>'like','page'=>0,'pid'=>'all','goodstype'=>'price');
	$return['red']=model('jy_merch/model')->get_myred();
	set_html('title',$return['pluginset']['title']?$return['pluginset']['title']:'在线'.$return['pluginset']['buytitle']);
	exi($return);
}
if($_RQ['op']=='getgoods'){
	//获取最近50家店
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$lat_lng=$this->session('lat_lng');
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
		'cateid'=>$merchgroups,
	);
	$pindex = max(1, 1);
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,10000),//获取所有
		'fields'=>array('id','merchname','lat_lng','logo','cateid'),
		'key'=>'id',
		'orderby'=>$orderby,
	);
	$merchs=model('jy_merch/model')->getmerch($parameter,6,$lat_lng,intval($_RQ['page']));
	$merchids=array();
	foreach($merchs as &$v){
			$v['onlinetime']=json_decode($v['onlinetime'],true);
			if($v['juli']<10){
				$v['peisongtime']=$v['juli']*8+28;
			}else if($v['juli']<30){
				$v['peisongtime']=$v['juli']*3+28;
			}else{
				$v['peisongtime']=$v['juli']*1+28;
			}
			$v['out_juli']=$v['out_juli']>0?$v['out_juli']:0;
			$merchids[]=$v['id'];
	}
	if(count($merchs)==0){
		exi(array('list'=>array()));
	}
	//当前时间段热门商品
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
		'nochecked'=>0,
		'merchid'=>$merchids,
	);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','merchid','title','image','price','active_plugin','active_id','salenum','originalprice','usermaxbuy','unit');
	$orderby='salerealnum desc ';
	if($_RQ['order']){
		if($_RQ['order']=='sales'){
			$orderby='salerealnum desc';
		}
		if($_RQ['order']=='price'){
			if($_RQ['order_type']==2){
				$orderby='price desc';
			}else{
				$orderby='price asc';
			}
			
		}
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'fields'=>$fields,
		'orderby'=>$orderby,
	);
	$list=model('jy_weishop/goods')->goods_list($parameter,$total);
	foreach($list as &$itemv){
		$itemv['originalprice']=$itemv['originalprice']?$itemv['originalprice']:price($itemv['price']*1.2);
		//按点赞数排序作品
		$pindex = max(1, 0);
		$members=pdo_getslice('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id']),array($pindex,5),$total,array('mid'),'','id desc');
		foreach($members as &$mv){
			$memberinfo=pdo_get('core_members',array('id'=>$mv['mid']),array('nickname','avatar'));
			$mv['nickname']=$memberinfo['nickname'];
			$mv['avatar']=$memberinfo['avatar'];
		}
		$itemv['members']=$members;
		$install = check_permission('jy_merch');
		if($install){
			$itemv['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$itemv['merchid']),'merchname');
		}
		$itemv['likes']=pdo_count('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id']),'id');
		$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
	}
	$return['list']=array_values($list);
	exi($return);
}
if($_RQ['op']=='getmerchs'){
	$showtype=intval($_RQ['showtype']);
	$type=intval($_RQ['type']);
	if(!$_RQ['cid'] && $type){
		$allcate=pdo_getall('jy_merch_category',array('type'=>$type,'uniacid'=>$_SESSION['uniacid']),'id','id');
		if($allcate){
			$merchgroups=array_keys($allcate);
		}else{
			$merchgroups=0;
		}
		if(count($merchgroups)==0){
			exi(array('list'=>array()));
		}
	}elseif($_RQ['cid']){
		$merchgroups=intval($_RQ['cid']);
		$return['params']['cid']=intval($_RQ['cid']);
	}
	//获取最近50家店
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$lat_lng=$this->session('lat_lng');
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
		'merchname <>'=>'',
		'lat_lng <>'=>'',
	);
	if($merchgroups){
		$params['cateid']=$merchgroups;
	}
	$orderby='isrecommand desc, star desc, salenum desc, id desc';
	//$orderbykey='isrecommand';
	if(trim($_RQ['order']) && trim($_RQ['order'])!='all'){
		$order=trim($_RQ['order']);
		if($order=='salenum'){
			$orderby='salenum desc, isrecommand desc, id desc';
			$orderbykey='salenum';
		}
		if($order=='sudu'){
			$orderby='sudu desc, isrecommand desc, salenum desc, id desc';
			$orderbykey='sudu';
		}
		if($order=='star'){
			$orderby='star desc, isrecommand desc, salenum desc, id desc';
			$orderbykey='star';
		}
		if($order=='brand'){
			$params['isbrand']=1;
		}
		if($order=='you'){
			$orderby='isrecommand desc, salenum desc, id desc';
			$orderbykey='isrecommand';
		}
		if($order=='out_pay'){
			$orderby='out_pay desc, isrecommand desc, salenum desc, id desc';
			$orderbykey='out_pay';
		}
		if($order=='out_price'){
			$orderby='out_price desc, isrecommand desc, salenum desc, id desc';
			$orderbykey='out_price';
		}
		if($order=='istuijie'){
			$orderby='salenum desc, id desc';
			$orderbykey='istuijie';
		}
		$return['params']['order']=trim($_RQ['order']);
	}
	if(intval($_RQ['star'])){
		$params['star >']=intval($_RQ['star'])-0.2;
		$return['params']['star']=intval($_RQ['star']);
	}
	if(trim($_RQ['keyword'])){
		$idsparams=$params;
		$idsparams['merchname like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
		$merchids=pdo_getall('jy_merch_user',$idsparams,'id','id');
		if($merchids){
			$merchids=array_keys($merchids);
			$params['id']=$merchids;
		}else{
			$goodsparams=array(
				'uniacid'=>$_SESSION['uniacid'],
				'status'=>1,
				'merchid >'=>0,
			);
			$goodsparams['title like']='%'.trim($_RQ['keyword']).'%';
			$merchids=pdo_getall('jy_weishop_goods',$goodsparams,'merchid','merchid');
			if($merchids){
				$merchids=array_keys($merchids);
				$params['id']=$merchids;
			}
		}
	}
	if($order=='istuijie'){
		$idsparams=$params;
		$idsparams['isrecommand']=1;
		$merchids=pdo_getall('jy_merch_user',$idsparams,'id','id');
		if($merchids){
			$merchids=array_keys($merchids);
			$params['id']=$merchids;
		}else{
			$params['id']=0;
		}
	}
	$pindex = max(1, 1);
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,10000),//获取所有
		'fields'=>array('id','star','isrecommand','merchname','out_price','lat_lng','logo','topimg','cateid','onlinetime'),
		'key'=>'id',
		'orderby'=>$orderby,
		'orderbykey'=>$orderbykey,
	);
	$merchs=model('jy_merch/model')->getmerch($parameter,$_RQ['limit'],$lat_lng,intval($_RQ['page']),intval($_RQ['juli']));
	$citypeisong=plugin('jy_weishop')->plugin_setting('citypeisong');
	foreach($merchs as $k=>&$v){
			$v['logo']=$v['logo']?tomedia($v['logo']):'';
			$v['topimg']=$v['topimg']?tomedia($v['topimg']):'';
			$v['onlinetime']=json_decode($v['onlinetime'],true);
			$v['onlinetime']['start']=$v['onlinetime']['start']?:'7:00';
			$v['onlinetime']['end']=$v['onlinetime']['end']?:'22:00';	
			$days=date('Y-m-d',TIMESTAMP);
			$star=strtotime($days.' '.$v['onlinetime']['start']);
			$end=strtotime($days.' '.$v['onlinetime']['end']);
			if($star>TIMESTAMP || TIMESTAMP>$end){
				$v['offline']=1;
			}
			if($v['juli']<10){
				$v['peisongtime']=$v['juli']*5+28;
			}else if($v['juli']<30){
				$v['peisongtime']=$v['juli']*3+28;
			}else{
				$v['peisongtime']=$v['juli']*1+28;
			}
			$v['out_juli']=$v['out_juli']>0?$v['out_juli']:8;
			$v['out_price']=$v['out_price']>0?$v['out_price']:$citypeisong['out_price'];
			if(($_RQ['getgoods'] && !$k) || ($order=='istuijie')){
				$pindex = max(1, 1);
				$psize = 3;
				$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'nochecked'=>0,'merchid'=>$v['id']);
				$fields=array('id','title','price','image','unit','originalprice','nochecked');
				$goods=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','salenum desc, id desc');
				foreach($goods as &$gv){
					$gv['image']=tomedia($gv['image']);
					$gv['originalprice']=$gv['originalprice']?:$gv['price']*1.2;
					$gv['originalprice']=price($gv['originalprice']);
					$v['tags']=pdo_getcolumn('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$gv['id']),'tags');
				}
				$v['goods']=$goods;
			}
			$v['coupons']=model('jy_weishop/coupon')->getmerchcoupon($v['id'],1);
			$v['salenum']=pdo_getsum('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$v['id']),'total');
			$goodsids=pdo_getall('jy_weishop_goods',array('merchid'=>$v['id']),'id','id');
			if($goodsids){
				$goodsids=array_keys($goodsids);
				$v['tags']=pdo_getcolumn('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsids),'tags');
			}
			$tags=str_array($return['pluginset']['tags'],'|');
			$key=mt_rand(0,count($tags)-1);
			$v['tags']=$v['tags']?:$tags[$key];	
	}
	$return['list']=array_values($merchs);
	exi($return);
}
if($_RQ['op']=='out_time'){
	$merchid=intval($_RQ['merchid']);
	$post['set_timebox']=$return['pluginset']['set_timebox'];
	$post['peisong_timebox']=$return['pluginset']['peisong_timebox'];
	$post['out_pay']=$return['pluginset']['out_pay'];
	$out_time = model('jy_merch/model')->out_times($merchid,$post);
	$timestamp = TIMESTAMP + 60 * 30;
	$data = model('jy_weishop/store')->array_order($timestamp, $out_time['timestamp']);
	$predict_index = array_search($data, $out_time['timestamp']);
	$predict_day = $out_time['days'][0];
	$predict_time = $out_time['times'][$predict_index]['start'].'~'.$out_time['times'][$predict_index]['end'];
	$text_time = "尽快送达";
	$return['timestamp']=$timestamp;
	$return['predict_time']=$predict_time;
	$return['out_time']=$out_time;
	exi($return);
}
if($_RQ['op']=='getprovince'){
	$return['province']=file_get_contents('core/resource/area.txt');
	$return['province']=unserialize($return['province']);
	exi($return);
}
if($_RQ['op']=='uppcd'){
	$post=get_postdata();
	if($post['lat'] || $post['lng']){
		$lat_lng=array('lat'=>$post['lat'],'lng'=>$post['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$this->session('pcd',$post);
	//print_r($this->session('pcd'));exit;
	exi('更新成功！');
}
function style($id)
{
	switch ($id%7) {	
		case 0: return "style-brown";
		case 1: return 'style-red';
		case 2: return "style-pink";
		case 3: return "style-orange";
		case 4: return "style-green";
		case 5: return "style-blue";
		case 6: return "style-purple";
		case 7: return "style-brown";
		default: }
}
	
?>